package com.zhuqi.configuration;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;

/**
 * @author : ZhuQi
 * @version : 时间:2022/8/9 10:34 版本:
 * 组装 File 路径
 */
@Component
@Data
@Slf4j
public class FilePathList {

    private ArrayList<String> list = new ArrayList<>(2048);

    @Autowired
    private PropertiesConfiguration propertiesConfiguration;

    public void getPaths() {
        String logPath = propertiesConfiguration.getPath();
        File file = new File(logPath);
        getPath(file);
        log.info("文件个数:{}", list.size());
    }

    private void getPath(File file){
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.getName().equals("error")) {
                    continue;
                }
                getPath(f);
            }
        } else {
            if (file.isFile()) {
                String name = file.getName();
                if (name.endsWith("gz")) {
                    return;
                }
                list.add(file.getAbsolutePath());
            }
        }
    }
}
